home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / Amster-source.lha / Amster_Install / Source / chatline.c < prev    next >
C/C++ Source or Header  |  2000-11-21  |  4KB  |  166 lines

  1. /*
  2. ** Chatline (String.mui Subclass with History)
  3. ** Copyright (C) 2000 Gurer Ozen <madcat@linuxfan.com>
  4. **
  5. ** This code is free software; you can redistribute it and/or
  6. ** modify it under the terms of GNU General Public License.
  7. */
  8.  
  9. #include "chatline.h"
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <exec/memory.h>
  16. #include <intuition/sghooks.h>
  17.  
  18. static ULONG chatline_new(struct IClass *cl, Object *obj, struct opSet *msg);
  19. static ULONG chatline_dispose(struct IClass *cl,Object *obj,Msg msg);
  20. static MUI_HOOK_DECL(chatline_edit, struct SGWork *sgw, ULONG *msg);
  21. static void chatline_add(struct chatlinedata *data, char *text);
  22.  
  23.  
  24. MUI_DISPATCH(chatline_dispatch)
  25. {
  26.     switch(msg->MethodID) {
  27.         case OM_NEW: return(chatline_new(cl, obj, (APTR)msg));
  28.         case OM_DISPOSE: return(chatline_dispose(cl, obj, (APTR)msg));
  29.         case CHATLINE_ADD:
  30.             chatline_add(INST_DATA(cl,obj), (char *)(((muimsg)msg)->arg1));
  31.             return 0;
  32.  
  33.  
  34.  
  35.     }
  36.     return(DoSuperMethodA(cl,obj,msg));
  37. }
  38.  
  39.  
  40. static ULONG chatline_new(struct IClass *cl, Object *obj, struct opSet *msg)
  41. {
  42.     static struct Hook editHook = {{0,0}, &chatline_edit, NULL, NULL};
  43.     struct chatlinedata *data;
  44.  
  45.     obj = (Object *)DoSuperNew(cl,obj,
  46.         StringFrame,
  47.         MUIA_String_EditHook, &editHook,
  48.         TAG_MORE, msg->ops_AttrList);
  49.  
  50.     if(!obj) return(0);
  51.  
  52.     data = INST_DATA(cl,obj);
  53.  
  54.     data->pool = CreatePool(MEMF_PUBLIC, 1024, 900);
  55.     if(!data->pool) {
  56.         CoerceMethod(cl, obj, OM_DISPOSE);
  57.         return 0;
  58.     }
  59.  
  60.     editHook.h_Data = (void *)data;
  61.  
  62.  
  63.     return((ULONG)obj);
  64. }
  65.  
  66.  
  67. MUI_HOOK_STATIC(chatline_edit, struct SGWork *sgw, ULONG *msg)
  68. {
  69.     if(*msg == SGH_KEY && sgw->EditOp == EO_NOOP && sgw->IEvent->ie_Class == IECLASS_RAWKEY)
  70.     {
  71.         struct chatlinedata *data = hook->h_Data;
  72.         UWORD code = sgw->IEvent->ie_Code;
  73.         UWORD qual = sgw->IEvent->ie_Qualifier;
  74.         int upd = 0;
  75.  
  76.         if(code == 0x4c && !(qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)))
  77.         {
  78.             /* up arrow */
  79.  
  80.             if(data->cur) {
  81.                 if(data->cur->prev) data->cur=data->cur->prev;
  82.             }
  83.             else
  84.                 data->cur = data->lastline;
  85.  
  86.             if(data->cur) upd=1;
  87.         }
  88.         else if(code == 0x4c && qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  89.         {
  90.             /* shift + up arrow */
  91.  
  92.             data->cur = data->lines;
  93.             upd = 1;
  94.         }
  95.         else if(code == 0x4d && !(qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)))
  96.         {
  97.             /* down arrow */
  98.  
  99.             if(data->cur) data->cur = data->cur->next;
  100.  
  101.             if(data->cur) {
  102.                 upd = 1;
  103.             } else {
  104.                 sgw->WorkBuffer[0] = '\0';
  105.                 sgw->NumChars = 0;
  106.                 sgw->BufferPos = 0;
  107.                 sgw->Actions |= SGA_REDISPLAY;
  108.             }
  109.         }
  110.         else if(code == 0x4d && qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  111.         {
  112.             /* shift + down arrow */
  113.  
  114.             data->cur = NULL;
  115.             sgw->WorkBuffer[0] = '\0';
  116.             sgw->NumChars = 0;
  117.             sgw->BufferPos = 0;
  118.             sgw->Actions |= SGA_REDISPLAY;
  119.         }
  120.  
  121.         if(upd) {
  122.             strcpy(sgw->WorkBuffer, &data->cur->line[0]);
  123.             sgw->NumChars = data->cur->len;
  124.             sgw->BufferPos = data->cur->len;
  125.             sgw->Actions |= SGA_REDISPLAY;
  126.         }
  127.     }
  128.  
  129.     return 0;
  130. }
  131.  
  132.  
  133. static ULONG chatline_dispose(struct IClass *cl,Object *obj,Msg msg)
  134. {
  135.     struct chatlinedata *data = INST_DATA(cl,obj);
  136.  
  137.     if(data->pool) DeletePool(data->pool);
  138.  
  139.     return(DoSuperMethodA(cl, obj, msg));
  140. }
  141.  
  142.  
  143. static void chatline_add(struct chatlinedata *data, char *text)
  144. {
  145.     struct chatlinestr *cl;
  146.     int len;
  147.  
  148.     if(!text) return;
  149.  
  150.     len = strlen(text);
  151.     cl = AllocPooled(data->pool, sizeof(struct chatlinestr) + len + 1);
  152.     if(!cl) return;
  153.     memset(cl, 0, sizeof(struct chatlinestr));
  154.     cl->len = len;
  155.     strcpy(&cl->line[0], text);
  156.  
  157.     if(data->lastline) {
  158.         data->lastline->next = cl;
  159.         cl->prev = data->lastline;
  160.     } else {
  161.         data->lines = cl;
  162.     }
  163.     data->lastline = cl;
  164.     data->cur = NULL;
  165. }
  166.